﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace WpfApp.SaveInfos
{
    public class SaveInfo
    {
        public static void Write(Object obj, string fileName)
        {
            var value = JsonSerializer.Serialize(obj);
            File.WriteAllText(fileName, value);
        }

        public static TObject? Read<TObject>(string fileName) where TObject : class
        {
            if (!File.Exists(fileName))
            {
                return default;
            }

            var text = File.ReadAllText(fileName);

            if (string.IsNullOrWhiteSpace(text))
            {
                text = "{}";
            }

            return JsonSerializer.Deserialize<TObject>(text);
        }
    }
}
